package com.imut.onlinebanking.user.controller;

import com.imut.onlinebanking.base.response.JsonResult;
import com.imut.onlinebanking.user.pojo.entity.Lose;
import com.imut.onlinebanking.user.service.LoseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class LoseController {

    @Autowired
    private LoseService loseService;

    // ✅ 新增一个独立接口用于返回 JSON 数据
    @GetMapping("/api/loses")
    @ResponseBody
    public List<Lose> getAllLoses() {
        return loseService.getAllLoses();
    }

    // ✅ 原来的 backlose 页面渲染保留
    @GetMapping("/backlose")
    public String showBacklose(Model model) {
        List<Lose> loses = loseService.getAllLoses();
        model.addAttribute("loses", loses);
        return "backlose";
    }


    // 展示挂失申请表单页面（可选）
    @GetMapping("/apply")
    public String showApplyForm() {
        return "lose"; // 对应 lose.html
    }

    // ✅ 新增：处理挂失申请提交
    @PostMapping("/apply")
    public String applyLose(@ModelAttribute Lose lose) {
        System.out.println("接收到的挂失信息: " + lose);
        loseService.insert(lose);

        // 重定向到 success.html 页面
        return "redirect:/success.html";
    }
}
